<template>

  <div :class="isShow === true ? 'commentList' : 'commentList2'">
    <!-- 挂载的时候不检查是否触底 -->
    <van-list v-model="loading" :finished="finished" finished-text="没有更多了" offset="50" @load="onLoad"
      :immediate-check="false">
      <div v-for="(item,i) in commentList" :key="item.com_id">
        <van-cell title="" size="large" :border="false" style="padding-bottom:0">
          <template #label>
            <div class="titleBox">
              <div class="left">
                <div>
                  <van-image class="img" fit="cover" round :src="item.aut_photo" />
                </div>
                <div class="textBox">
                  <div>{{ item.aut_name }}</div>
                </div>
              </div>

              <div class="right">
                <div v-if="item.is_liking === true" @click="liking(true, i,item.com_id)">
                  <van-icon name="like" size="large" color="red" />
                </div>
                <div v-else @click="liking(false, i,item.com_id)">
                  <van-icon name="like-o" size="large" />
                </div>
              </div>
            </div>
          </template>
        </van-cell>

        <van-cell title="" size="large" :border="false" style="padding-top:0">
          <template #label>
            <div class="text1">{{ item.content }}</div>
            <div class="text2">{{ $utils.timeAgo(item.pubdate) }}</div>
          </template>
        </van-cell>
      </div>
    </van-list>
    <!-- <van-divider>没有更多了</van-divider> -->

    <div class="comBox">
      <div class="comBox1" v-if="isShow === false">
        <van-icon name="arrow-left" color="#000" style="margin-left:0" />
        <div class="textBox" @click="ShowFn()">{{ message }}</div>
        <div class="iconBox">
          <van-badge :content="count === 0 ? '' : count" max="99">
            <van-icon name="comment-o" @click="comClickFn()" />
          </van-badge>
          <van-icon name="star-o" v-if="collected === false" @click="collectedFn(false)" />
          <van-icon name="star" v-else @click="collectedFn(true)" />
          <van-icon name="share-o" @click="showShare = true" />
        </div>
      </div>

      <div class="comBox2" v-else>
        <!-- 动态绑定去掉空格 -->
        <textarea v-fofo v-model.trim="message" placeholder="友善评论、理性发言、阳光心灵" @blur="blurFn()"></textarea>
        <van-button type="default" :disabled="message.length === 0" @click="send()">发布</van-button>
      </div>
    </div>

    <van-share-sheet v-model="showShare" title="立即分享给好友" :options="options" @select="onSelect" />
  </div>
</template>

<script>
import debounce from '@/utils/debounce.js'
import { Toast } from 'vant'
import Notify from '@/ui/Notify'
export default {
  props: {
    is_collected: Boolean
  },
  created () {
    this.getData()
  },
  watch: {
    is_collected (newVal, oldVal) {
      this.collected = newVal
    }
  },
  data () {
    return {
      offset: null,
      commentList: [],
      message: '请输入留言',
      isShow: false,
      count: 0,
      loading: false,
      finished: false,
      showShare: false,
      collected: false,
      options: [
        { name: '微信', icon: 'wechat' },
        { name: '微博', icon: 'weibo' },
        { name: '复制链接', icon: 'link' },
        { name: '分享海报', icon: 'poster' },
        { name: '二维码', icon: 'qrcode' }
      ]
    }
  },
  methods: {
    async getData () {
      const { data: res } = await this.$api.commentsAPI({ type: 'a', source: this.$route.query.art_id, offset: this.offset })
      this.count = res.data.total_count
      this.commentList = [...this.commentList, ...res.data.results]
      this.offset = res.data.last_id
      if (this.offset === null) {
        this.finished = true
      }
      this.loading = false
    },
    liking: debounce(async function (e, i, comid) {
      if (e === false) {
        await this.$api.commentlikingsAPI({ target: comid })
        this.commentList[i].is_liking = true
      } else {
        await this.$api.commentDelApi(comid)
        this.commentList[i].is_liking = false
      }
    }, 500),
    ShowFn () {
      this.isShow = true
      this.message = ''
    },
    comClickFn () {
      // 参数1是水平,参数2是垂直
      // window.scrollTo(0, this.$refs.commentList.offsetTop - 60)
      // JS内置滚动带动画功能
      document.querySelector('.likeBox').scrollIntoView({
        behavior: 'smooth'
      })
    },
    blurFn () {
      setTimeout(() => {
        this.isShow = false
        this.message = '请输入留言'
      }, 0)
    },
    async send () {
      const { data: res } = await this.$api.commentsendAPI({ target: this.$route.query.art_id, content: this.message })
      Notify({ type: 'success', message: res.message })
      // 或者unshift在数组最前面添加
      this.commentList.unshift(res.data.new_obj)
      this.count += 1
      // this.getData()
      setTimeout(() => {
        this.comClickFn()
      }, 100)
    },
    onLoad () {
      if (this.commentList.length === 0) {
        this.loading = false
      } else {
        this.getData()
      }
    },
    onSelect (option) {
      Toast(option.name)
      this.showShare = false
    },
    collectedFn: debounce(async function (e) {
      if (e === false) {
        const { data: res } = await this.$api.articlecollectionsApi({ target: this.$route.query.art_id })
        Notify({ type: 'success', message: res.message })
        this.collected = true
      } else {
        this.$api.articlecollectionsDelApi(this.$route.query.art_id)
        this.collected = false
      }
    }, 500)
  }
}
</script>

<style lang="less" scoped>
.img {
  width: 40px;
  height: 40px;
}

.commentList2 {
  padding-bottom: 50px;
}

.commentList {
  padding-bottom: 80px;
}

.titleBox {
  display: flex;
  justify-content: space-between;
  align-items: center;

  .left {
    display: flex;
    align-items: center;
    color: #000;
    font-weight: 400;

    .textBox {
      margin-left: 5px;

      div {
        margin-bottom: 2px;
      }
    }
  }
}

.text1,
.text2 {
  text-align: left !important;
  text-indent: 2em;
}

.text1 {
  font-size: 17px;
  margin-bottom: 15px;
  color: #000;
}

.text2 {
  display: flex;
  justify-content: left;
  text-indent: 0 !important;
}

.comBox {
  position: fixed;
  width: 100%;
  bottom: 0;
  padding: 10px 0;
  background: #e8ebf3;

  .comBox1 {
    display: flex;
    align-items: center;
    padding: 0 10px;
    justify-content: space-between;

    .textBox {
      font-size: 16px;
      background: #d0d3db;
      padding: 5px 10px;
      width: 180px;
      text-align: left;
      border-radius: 15px;
    }

    .van-icon {
      font-size: 25px;
      margin-left: 15px;
    }

  }

  .comBox2 {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 10px;

    textarea {
      width: 280px;
      height: 50px;
      font-size: 14px;
      resize: none;
    }
  }
}
</style>
